package com.hdw.common.util;

import com.hdw.common.vo.LayTreeVO;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TreeUtil {

    public static List<LayTreeVO> treeHandler(Map<String,LayTreeVO> treeMap) {
        List<LayTreeVO> list = new ArrayList<>();
        LayTreeVO treeVO,parentVO;
        for(Map.Entry<String,LayTreeVO> entry:treeMap.entrySet()) {
            treeVO = entry.getValue();
           if(StringUtils.isEmpty(treeVO.getParent())) {
               list.add(treeVO);
           } else {
               parentVO = treeMap.get(treeVO.getParent());
               if(parentVO!=null) {
                   if(parentVO.getChildren()==null) {
                       parentVO.setChildren(new ArrayList<>());
                   }
                   parentVO.getChildren().add(treeVO);
               }
           }
        }
        return list;
    }

}
